Perl stands for Practical Extraction and Report Language. Perl has traditionally been used within Unix for automating administration tasks. It is particularly suited to these tasks because programs which do string manipulation can be written quickly (lazily) and without the overhead of memory management etc. Perl is particularly good at pattern matching (ala sed/awk) string manipulation and hashing functions. As sometimes happens, the strengths of Perl have matched the needs of an emerging programming domain. The growth of the World Wide Web has produced a stateless(?) client-server architecture where Perl is particularly useful.
Web server developers allow users to modify the functionality of their web servers by changing configuration files or even adding completely new functions to the server. A variety of interfaces have been developed to extend server functionality including APIs, plug-in modules and an interface known as the Common Gateway Interface (CGI). Over the past few years, people have developed a tremendous range of applications using this interface and these applications have helped catapault the WWW to it's current level of popularity.
Programs which work within the context of CGI can be written in any language (often in shell scripts, C or C++, but increasingly in database languages, lisp, tcl, Java etc.), but Perl remains one of the primary choices for programming in this context. This tutorial focuses on using Perl to program in the CGI environment both because this is an effective combination of language and domain, but also because domain and language ground one another. Talking about CGI without also talking about a programming language is vacuous. And the reverse is frankly too large in scope to be useful.
The functionality of a web server can be extended through the use of the CGI interface. (CGI was originally created on Unix systems. Therefore an understanding of Unix is very useful in understanding CGI. This tutorial assumes a general knowledge of Unix, of the Web and of HTTP servers. This may be overly optimistic... Oh well.) When a web server receives a request for a resource, it typically looks for cooresponding file on the server's file system. The server determines some various meta-information, and then sends both the meta-info and content of the file to the requestor.
Sometimes, the requested resource is interpereted as a CGI resource. When this happens, the file is not read but executed. Parameters are passed through the command-line interface, standard input and through environment variables. Output from the executable is passed along to the server and then sent out to the requestor. The client request is essentially generated by an executable program.
Generally, the output of a CGI executable is text in HTML. However, the output can be plain text, an image or any standard (or proprietary) MIME type. For the most part, this tutorial focuses on the problem of HTML output and lets the reader generalize to other media types.
should execute the perl instructions in the file.
Users are often insulated from knowing this detail. Most shells support the following convention -- if the first line of an executable text file starts with #!, than the remainder of the line is used as the interpretor for the file. Therefore, all the perl program examples given here will follow the following form.
#!/usr/local/bin/perl print "Hello World\n";
#!/usr/local/bin/perl # This is a Comment that will not be interpreted print "Hi\n"; # This is also comment
Hi
#!/usr/local/bin/perl $String = "Watch this"; $Num = 6; $Result = $Num * 2; print "$String $Num * 2 = $Result\n";
Watch this 6 * 2 = 12Note: the values of the scalars are substituted for the scalars within double quotes. No interpretation is done within single quotes.
#!/usr/local/bin/perl $This = "Watch"; print "$This\n"; print '$This\n';
Watch $This
#!/usr/local/bin/perl while(<STDIN>) {print;}
BlahBlah Blah
BlahBlah Blah
Arrays are represented by prefixing them with the @ symbol. The array MyList is written @MyList.
@MyList = ("hi", "there"); @AList = (); # Empty list, $#BList = -1 @BList = (1, 2, @AList, 3); # @CList = (1, 2, 3) @CList = (1, 2, @MyList, 3); # @AList = (1, 2, "hi", "there", 3)The size of the array may grow and shrink by adding and removing elements from the array.
%MyList = ( "dog", "big", "cat", "small", "horse", "huge", "guppy", "miniscule");After this assignment, $MyList{"dog"} returns "big".
$MyList{"whale"} = "humongous"This assignment adds the association "whale"=> "humongous" to the associative array %MyList.